// imported requirements
include(jslib_io);
include(jslib_prefs);
include(jslib_dirutils);
include(jslib_fileutils);

// delete selected script from scriptsManager.xul view
function commandDeleteScript() {
	var stringBundle = document.getElementById('stringBundle');
	var container = document.getElementById('container');
	var scriptsListBox = container.contentWindow.document.getElementById('scriptsListBox');

	if (scriptsListBox.selectedIndex < 0) {
		alert(stringBundle.getString('noScriptSelected'));
		return;
	}

	if (!confirm(stringBundle.getString('deleteScriptConfirm')))
		return;

	var scripts = TH_getInfo('scripts');

	var stringBundle = container.contentWindow.document.getElementById('stringBundle');

	TH_removeScript(scripts[scriptsListBox.selectedIndex]);

	helper_populateScripts(scriptsListBox, stringBundle);
}

// modify selected script from scriptsManager.xul view
function commandModifyScript() {
	var stringBundle = document.getElementById('stringBundle');
	var container = document.getElementById('container');
	var scriptsListBox = container.contentWindow.document.getElementById('scriptsListBox');

	if (scriptsListBox.selectedIndex < 0) {
		alert(stringBundle.getString('noScriptSelected'));
		return;
	}

	var pref = new Prefs();
	var editor = pref.getChar('technika.editor');

	if (!editor)
		commandSelectEditor();

	var editor = pref.getChar('technika.editor');

	if (!editor) {
		alert(stringBundle.getString('noEditorSelected'));
		return;
	}

	var scripts = TH_getInfo('scripts');

	var fu = new FileUtils;
	fu.run(editor, [scripts[scriptsListBox.selectedIndex].path]);
}

// create script
function commandCreateScript() {
	var stringBundle = document.getElementById('stringBundle');

	var du = new DirUtils;
	var fu = new FileUtils;

	var tmp = du.getTmpDir();
	var filePath = fu.append(tmp, 'technika.script.tmp');

	if (fu.exists(filePath))
		fu.remove(filePath);

	var file = new File(filePath);
	file.open('w');
	file.write("/*\r\n@name Script\r\n@version 0\r\n@description ** TODO **\r\n*/");
	file.close();

	var pref = new Prefs();
	var editor = pref.getChar('technika.editor');

	if (!editor)
		commandSelectEditor();

	var editor = pref.getChar('technika.editor');

	if (!editor) {
		alert(stringBundle.getString('noEditorSelected'));
		return;
	}
	
	fu.run(editor, [filePath]);
	
	// how do we detect when the process end?
}

// install script
function commandInstallScript() {
	var stringBundle = document.getElementById('stringBundle');
	var nsIFilePicker = Components.interfaces.nsIFilePicker;

	var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
	fp.init(window, stringBundle.getString('installScriptSelectDialogTitle'), nsIFilePicker);
	fp.appendFilters(nsIFilePicker.filterText | nsIFilePicker.filterAll);
	fp.appendFilter('JavaScript Files','*.js');

	var res = fp.show();

	if (res == nsIFilePicker.returnOK) {
		scriptPath = fp.file.path;

		var file = new File(scriptPath);
		file.open();

		var source = file.read();

		file.close();

		try {
			var script = TH_parseScript(source);
			TH_createScript(script);
		} catch(e) {
			if (e == 'incorrect format')
				alert(stringBundle.getString('incorrectScriptFormat'));
			else if (e == 'script exists')
				alert(stringBundle.getString('scriptExists'));
			else
				alert(stringBundle.getString('scriptCannotBeInstalled'));
		}
	}

	var container = document.getElementById('container');
	var scriptsListBox = container.contentWindow.document.getElementById('scriptsListBox');
	var stringBundle = container.contentWindow.document.getElementById('stringBundle');

	helper_populateScripts(scriptsListBox, stringBundle);
}

// disable all scripts from scriptsManager.xul view
function commandDisableScripts() {
	var container = document.getElementById('container');
	var scriptsListBox = container.contentWindow.document.getElementById('scriptsListBox');
	var stringBundle = container.contentWindow.document.getElementById('stringBundle');
	
	TH_disableAllScripts();
	
	helper_populateScripts(scriptsListBox, stringBundle);
}

// select default script editor
function commandSelectEditor() {
	var stringBundle = document.getElementById('stringBundle');
	var nsIFilePicker = Components.interfaces.nsIFilePicker;

	var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
	fp.init(window, stringBundle.getString('selectEditorSelectDialogTitle'), nsIFilePicker);
	fp.appendFilters(nsIFilePicker.filterText | nsIFilePicker.filterAll);

	var res = fp.show();

	if (res == nsIFilePicker.returnOK) {
		editor = fp.file.path;

		var pref = new Prefs();
		pref.setChar('technika.editor', editor);
	}
}